Define Compositions & Services
The Compositions and Services definitions, along with their names and parameters, are placed in the header file to keep the contract organized and easily accessible for integration. This setup allows key structures and functions to be reused across modules without duplicating code.
Open your contract’s header file (database.hpp). Add the composition definition for container inside .
- Full Code
- Define Composition
- Define Services
database.hpp
#pragma once
#include <inery/inery.hpp>
using namespace inery;
CONTRACT database : public contract {
public:
using contract::contract;
database(name receiver, name code, datastream<const char*> ds)
: contract(receiver, code, ds) {}
// Define the container composition
TABLE container_struct {
uint64_t id; // Primary key
std::string data; // Data field
uint64_t primary_key() const { return id; }
};
// Declare the indexed composition
typedef inery::multi_index<"container"_n, container_struct> container_table;
// Action to upsert an entry
ACTION upsert(name user, uint64_t id, std::string data);
// Action to delete an entry
ACTION deleteentry(name user, uint64_t id);
};
database.hpp
// Define the container composition
TABLE container_struct {
uint64_t id; // Primary key
std::string data; // Data field
uint64_t primary_key() const { return id; }
};
// Declare the indexed composition
typedef inery::multi_index<"container"_n, container_struct> container_table;
database.hpp
// Action to upsert an entry
ACTION upsert(name user, uint64_t id, std::string data);
// Action to delete an entry
ACTION deleteentry(name user, uint64_t id);
Implement the Services Logic
Open your contract’s source file (database.cpp). Implement the upsert action, which will insert a new record if id does not exist or update the data field if it does.
Here’s the implementation:
- Full Code
- Upsert Service
- Delete Service
database.cpp
#include "database.hpp"
ACTION database::upsert(name user, uint64_t id, std::string data) {
// Require authorization of the user executing the action
require_auth(user);
// Access the container composition
container_table _container(get_self(), get_self().value);
// Find the entry by primary key (id)
auto itr = _container.find(id);
if (itr == _container.end()) {
// If id does not exist, create a new record
_container.emplace(user, [&](auto& row) {
row.id = id;
row.data = data;
});
} else {
// If id exists, modify the existing record
_container.modify(itr, user, [&](auto& row) {
row.data = data;
});
}
}
ACTION database::deleteentry(name user, uint64_t id) {
// Require authorization of the user executing the action
require_auth(user);
// Access the container composition
container_table _container(get_self(), get_self().value);
// Find the entry by primary key (id)
auto itr = _container.find(id);
// Check if the entry exists
check(itr != _container.end(), "Record with the given ID does not exist");
// Delete the entry
_container.erase(itr);
}
database.cpp
ACTION database::upsert(name user, uint64_t id, std::string data) {
// Require authorization of the user executing the action
require_auth(user);
// Access the container composition
container_table _container(get_self(), get_self().value);
// Find the entry by primary key (id)
auto itr = _container.find(id);
if (itr == _container.end()) {
// If id does not exist, create a new record
_container.emplace(user, [&](auto& row) {
row.id = id;
row.data = data;
});
} else {
// If id exists, modify the existing record
_container.modify(itr, user, [&](auto& row) {
row.data = data;
});
}
}
database.cpp
ACTION database::deleteentry(name user, uint64_t id) {
// Require authorization of the user executing the action
require_auth(user);
// Access the container composition
container_table _container(get_self(), get_self().value);
// Find the entry by primary key (id)
auto itr = _container.find(id);
// Check if the entry exists
check(itr != _container.end(), "Record with the given ID does not exist");
// Delete the entry
_container.erase(itr);
}